﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using TestMethod.Test;

namespace TestMethod
{
    public partial class TestFrm : Form
    {
        public TestFrm()
        {
            InitializeComponent();
        }

        TESTDao dal = new TESTDao();
        private TEST EditData = null;
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvMain.SelectedRows.Count > 0)
                {
                    DataGridViewRow dgv = dgvMain.SelectedRows[0];
                    if (dgv.IsNewRow)
                    {
                        TEST tmp = (TEST)dgv.DataBoundItem;
                        tmp.ID1 = "";
                        
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvMain.SelectedRows.Count > 0)
                {
                    DataGridViewRow dgv = dgvMain.SelectedRows[0];
                    if (!dgv.IsNewRow)
                    {
                        TEST tmp = (TEST)dgv.DataBoundItem;
                        dal.Delete(tmp);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                EditData.REMARK = txtRemark.Text.Trim();
                EditData.NAME1 = txtName.Text.Trim();
                EditData.AGE1 = txtAge.Text.Trim();
                dal.Update(EditData);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                MessageBox.Show(ex.Message);
            }

        }

        private void TestFrm_Load(object sender, EventArgs e)
        {
            try
            {
                colTxtAge.DataPropertyName = TEST.colAGE1;
                colTxtID.DataPropertyName = TEST.colID1;
                colTxtName.DataPropertyName = TEST.colNAME1;
                List<TEST> list = dal.GetAll();
               dgvMain.DataSource = list;

            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                MessageBox.Show(ex.Message);
            }
        }

        private void dgvMain_SelectionChanged(object sender, EventArgs e)
        {
            if(dgvMain.SelectedRows.Count>0)
            {
                DataGridViewRow dgv = dgvMain.SelectedRows[0];
                EditData = (TEST)dgv.DataBoundItem;
                txtAge.Text = EditData.AGE1;
                txtName.Text = EditData.NAME1;
                txtRemark.Text = EditData.REMARK;
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                TEST tmp = new TEST();
                tmp.REMARK = txtRemark.Text.Trim();
                tmp.NAME1 = txtName.Text.Trim();
                tmp.AGE1 = txtAge.Text.Trim();
                tmp.ID1 = "";
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                MessageBox.Show(ex.Message);
            }

        }
    }
}
